<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\Subscribe;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;

final class SubscribeAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_subscribe';
    protected $baseRoutePattern = 'subscribe';
    protected $maxPageLinks = 10;

    protected function configureDefaultSortValues (array &$sortValues) {
        $sortValues['_page'] = 1;
        $sortValues['_per_page'] = 32;
        $sortValues['_sort_order'] = 'DESC';
        $sortValues['_sort_by'] = 'id';
    }

    public function toString ($object) {
        return $object instanceof Subscribe ? $object->getEmail() : '订阅';
    }

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('email', null, ['label' => '邮箱']);
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->add('email', null, [
                'label' => '邮箱',
                'header_style' => 'width: 60%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('ip', null, [
                'label' => 'IP',
                'header_style' => 'width: 15%; text-align: center',
                'row_align'    => 'center',
            ])
            ->add('created_at', null, [
                'format'   => 'Y-m-d H:i:s',
                'timezone' => 'PRC',
                'label'    => '订阅时间',
                'header_style' => 'width: 15%; text-align: center',
                'row_align'    => 'center',
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->add('id')
            ->add('email')
            ->add('ip')
            ->add('status')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('email')
            ->add('ip')
            ->add('status')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub
        $collection->clearExcept(['list', 'delete', 'batch']);
    }
}
